var CPC_GET_URL		= "http://localhost:8080/getRecommendedCouponJson.php";
var HOME_URL		= "http://www.hohobb.com";

var getRetryCount 	= 0;
var recommendedPage	= 0;

var arrCache = new Array();



function callRecommended(page) {
	var coupons;
	
	var requestObj = new Object();
	requestObj.page = page;

// console.info("===>" + $.toJSON(requestObj));
//	console.info("page: " + page);
	
	var postFun = $.post(CPC_GET_URL, {'page':page});
	
	postFun.done(function(data, textStatus) {
//		console.info(data);
		
		var oCoupons = $.parseJSON(data);
		
		console.info(oCoupons);
		
		arrCache[page] = oCoupons;
//		writeRecommendedCoupons(oCoupons, HOME_URL, 'yes');
		test(oCoupons);	
	});

	postFun.fail(function(textStatus) {
		getRetryCount++;
		console.error(textStatus + "  callRecommended(): Unable to access recommended coupons: " + getRetryCount);
//		// try to relogin adwords and keep the tab alive.
//		if (getRetryCount > 60) {
//			myCloseWindow();
//		} else {
//			setTimeout("callcpcservices()", CALLCPCSERVICES_INTERVAL);
//		}
		return;
	});
}

function getMoreRecommended()
{
	recommendedPage++;
	
	 callRecommended(recommendedPage);

//	console.info("count: " + arrCache.length);
//	console.info("recommendedPage: " + recommendedPage);
//	console.info("data: " + arrCache);
//
//	if(arrCache[recommendedPage] != undefined){
//		test(arrCache[recommendedPage]);
//	}
}

function test(oCoupons)
{
	var oCoupon;
	var strCoupon;
	var oUl = $("#recommended_list");
	
	for(var i = 0, l = oCoupons.length; i < l; i++) {
		oCoupon = oCoupons[i];
		
		console.info( "id: " + oCoupon.id);
		
		strCoupon = "<li>" + oCoupon.id + "</li>";
		
		oUl.append(strCoupon);
		
	}
}

function writeRecommendedCoupons(oCoupons, homeUrl, isCategory) 
{
	var oCoupon;
	var strCoupon;
	
	for(var i = 0, l = oCoupons.length; i < l; i++) {
		oCoupon = oCoupons[i];
		
		console.info( "id: " + oCoupon.id);
		
		strCoupon = "<li>";
		strCoupon += "	<div class=\"thumbnailBox\">";
		strCoupon += "		<div class=\"thumbnail\">";
		
		if(oCoupon.cType == 1){
			strCoupon += "	  	<a title=\"" + oCoupon.name + "\" id=\"thumb_" + oCoupon.id + "\" href=\"" + oCoupon.itUrl + "\" class=\"highslide\" onclick=\"return hs.expand(this)\" rel=\"nofollow\" target=\"_blank\">";
			strCoupon += "         	<img src=\"" + homeUrl + "/images/url/140/80/" + oCoupon.baseItUrl + "\" ";
			strCoupon += "            	border=\"0\" width=\"140\" height=\"80\" alt=\"" + oCoupon.name + " - Hoho coupons\" title=\"" + oCoupon.name + "\" /></a>";
		}else if(oCoupon.cType == 2){
			strCoupon += "     	<a title=\"" + oCoupon.name + "\" href=\"" + homeUrl + "/" + oCoupon.mNameId + "-coupon-codes\" rel=\"nofollow\" target=\"_blank\">";
			strCoupon += "         	<img src=\"" + homeUrl + "/images/logo/140/80/" + oCoupon.logoId + "/" + oCoupon.mNameId + "-120x30-logo-HoHoBB." + oCoupon.logoEname + "\"  ";
			strCoupon += "	            border=\"0\" width=\"140\" height=\"80\" alt=\"" + oCoupon.name + " - Hoho coupons\" title=\"" + oCoupon.name + "\" /></a>";
		}
		
		strCoupon += "		</div>";
		strCoupon += "	</div>";
			        
		strCoupon += "  <div class=\"item-info\">";
		
		if(oCoupon.code != ""){
			strCoupon += "<div class=\"couponCode\"><span class=\"code\">" + oCoupon.code + "</span></div>";
			strCoupon += "<div class=\"item-title font13\">";
			strCoupon += "		<h3><a title=\"" + oCoupon.name + "\" href=\"" + homeUrl + "/redir.php?id=" + oCoupon.id + "&URL=" + oCoupon.baseUrl + "\">" + oCoupon.name + "<img src=\"" + homeUrl + "/images/button/trim.png\" width=\"1\" height=\"1\" /></a></h3></div>";
		}else{
			strCoupon += "<div class=\"item-title font13\">";
			strCoupon += "		<h3><a title=\"" + oCoupon.name + "\" href=\"" + homeUrl + "/redir.php?id=" + oCoupon.id + "&URL=" + oCoupon.baseUrl + "\">" + oCoupon.name + "<img src=\"" + homeUrl + "/images/button/trim.png\" width=\"1\" height=\"1\" /></a></h3></div>";
		}
		
		if(oCoupon.desc != ""){
			strCoupon += "<div class=\"itemInterval  font12 item-description\">	" + oCoupon.desc + "</div>";
		}
		
		if(oCoupon.typeStr != ""){
			strCoupon += "<div class=\"itemInterval greenLinkColor font11\">" + oCoupon.typeStr + "</div>";
		}
		
		if(isCategory != "yes"){
			if(oCoupon.cname != ""){
				strCoupon += "<DIV class=\"category itemInterval font11\">";
				strCoupon += "	Category: <a href=\"javascript:void(0);\" onclick=\"requestPage('/categories/" + oCoupon.fname + "-coupon-codes', " + oCoupon.cid + ", 1);\">" + oCoupon.cname + "</a>";
				strCoupon += "</DIV>";
			}
		}
						
		strCoupon += "<div class=\"more itemInterval font12\">More"; 
		strCoupon += " 	<a href=\"" + homeUrl + "/" + oCoupon.mNameId + "-coupon-codes\">" + oCoupon.mname + " coupon codes</a>";
			  
		if(oCoupon.ipixedl != ""){
			strCoupon += "	<img width=\"1\" height=\"1\" src=\"" + oCoupon.ipixedl + "\">";
		}
		
		strCoupon += "		</div>";                   
		strCoupon += "	</div>";
		strCoupon += "</li>";
	}
	
}